
package com.xiaoma.myaudience.biz.view;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;

import com.xiaoma.myaudience.R;

/**
 * 指示器View.<br/>
 * 可以在此类基础上,扩展实现各种样式指示器<br/>
 * 
 * @author wjying
 */
public abstract class Indicator extends LinearLayout implements AnimationListener {
    private int VISIBLE_TIME = 300;

    private int mTotalItems = -1;

    private int mCurrentItem = -1;

    private int mVisibleTime = -1;

    private Animation mAnimation;

    private Handler mHandler = new Handler();

    public Indicator(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public Indicator(Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {
        setFocusable(false);
    }

    /**
     * 当指示器位置需要移动时调用
     * 
     * @param r
     */
    public void move(float r) {

    }

    /**
     * 返回总数
     * 
     * @return
     */
    public int getTotalItems() {
        return mTotalItems;
    }

    /**
     * 设置总数
     * 
     * @param totalItems
     */
    public void setTotalItems(int totalItems) {
        if (totalItems != mTotalItems) {
            mTotalItems = totalItems;
            onSetTotalItems();
            // setVisibility(View.VISIBLE);
            mHandler.removeCallbacks(mAutoHide);
            if (mVisibleTime > 0)
                mHandler.postDelayed(mAutoHide, mVisibleTime);
        }
        mCurrentItem = -1;
    }

    /**
     * 返回当前指示位置
     * 
     * @return
     */
    public int getCurrentItem() {
        return mCurrentItem;
    }

    /**
     * 设置当前指示位置
     * 
     * @param currentItem
     */
    public void setCurrentItem(int currentItem) {
        if (currentItem < 0 || currentItem >= mTotalItems) {
            return;
        }
        if (currentItem != mCurrentItem) {
            mCurrentItem = currentItem;
            onSetCurrentItem();
            // setVisibility(View.VISIBLE);
            mHandler.removeCallbacks(mAutoHide);
            if (mVisibleTime > 0)
                mHandler.postDelayed(mAutoHide, mVisibleTime);
        }
    }

    /**
     * 当设置总数时调用
     * 
     */
    protected abstract void onSetTotalItems();

    /**
     * 当设置当前位置时调用
     * 
     */
    protected abstract void onSetCurrentItem();

    /**
     * 设置指示器是否需要自动隐藏
     * 
     * @param autohide
     */
    public void setAutoHide(boolean autohide) {
        if (autohide) {
            mVisibleTime = VISIBLE_TIME;
            setVisibility(INVISIBLE);
        } else {
            mVisibleTime = -1;
            setVisibility(VISIBLE);
        }
    }

    /**
     * 自动隐藏Runnable
     */
    private Runnable mAutoHide = new Runnable() {
        public void run() {
            if (mAnimation == null) {
                mAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.fade_out_fast);
                mAnimation.setAnimationListener(Indicator.this);
            } else {
                try {
                    // This little thing seems to be making some androids piss
                    // off
                    // if (!mAnimation.hasEnded())
                    // mAnimation.
                } catch (NoSuchMethodError e) {
                }
            }
            startAnimation(mAnimation);
        }
    };

    public void onAnimationEnd(Animation animation) {
        setVisibility(View.INVISIBLE);
    }

    public void onAnimationRepeat(Animation animation) {
    }

    public void onAnimationStart(Animation animation) {
    }
}
